package com.ruoyi.system.outto.goods.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttrTemplateMapper;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoImgMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.interAttr.domain.InterAttr;
import com.ruoyi.system.interAttr.mapper.InterAttrMapper;
import com.ruoyi.system.model.domain.*;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.dto.HttpUtil;
import com.ruoyi.system.outto.goods.TAOHAOHAOAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author sdq
 * @Date 2022/7/5 10:27
 */
@Slf4j
@Service
public class TAOHAOHAOAPIImpl implements TAOHAOHAOAPI {
    @Resource
    private GameAttrTemplateMapper gameAttrTemplateMapper;
    @Resource
    private GameAttributeMapper gameAttributeMapper;
    @Resource
    private GoodsInfoImgMapper goodsInfoImgMapper;
    @Resource
    private ArtRecMapper artRecMapper;
    @Resource
    InterAttrMapper interAttrMapper;


    public static String getCover(Map<String, Object> params) {
        StringBuffer resut = new StringBuffer();
        for (String key : params.keySet()) {
            if (params.get(key) instanceof String && StringUtils.isNotEmpty((String) params.get(key))) {
                resut.append(key + "=" + params.get(key) + "&");
            }
            if (params.get(key) instanceof Boolean) {
                resut.append(key + "=" + params.get(key) + "&");
            }
            if (params.get(key) instanceof Long) {
                resut.append(key + "=" + params.get(key) + "&");
            }
            if (params.get(key) instanceof Integer && null != params.get(key)) {
                resut.append(key + "=" + params.get(key) + "&");
            }
        }
        return resut.toString();
    }

    private String reIds(String allIds, String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            if (StringUtils.isNotEmpty(allIds)) {
                allIds = allIds + "," + ids;
            } else {
                allIds = ids;
            }
        }
        return allIds;
    }

    private List<GameAttribute> getGameAttributs(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        return gameAttributeMapper.selectGameAttributeListByIds((Long[]) ConvertUtils.convert(ids.split(","),
                Long.class));
    }

    static String getBusinessId(SalePartner salePartner, String gameId){
        String business_id = "";
        String url = "http://api.hhaohao.com/open/all_game";
        Map<String, String> params = new HashMap<>();
        params.put("open_id", salePartner.getTwoOther());
        params.put("key", salePartner.getSaleKey());
        String result = HttpUtil.doGet(url, params, null);
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        for (JSONObject jsonObject : jsonObjects) {
            if (Objects.equals(jsonObject.getString("game_id"), gameId)) {
                business_id = jsonObject.getString("business_id");
                break;
            }
        }
        return business_id;
    }

    /**
     * 开始匹配输入框属性
     *
     * @param goodsInfoAndAttrValueDto
     * @param params
     * @param inputAttr
     */
    private void builderInputAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, Map params, String inputAttr, Map<String, String> properties) {
//        JSONArray propertys = Objects.isNull(params.get("property")) ? new JSONArray() : JSONArray.parseArray(params.get("property").toString());
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();

        //========================开始匹配输入框=====================//
        JSONObject jsonObject1 = JSONObject.parseObject(inputAttr);
        String inputJson = jsonObject1.getString("inputJson");
        List<JSONObject> jsonObjects1 = JSONObject.parseArray(inputJson, JSONObject.class);
        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        ArrayList types = new ArrayList<>();
        types.add("number");
        types.add("input");
        gameAttrTemplate.setFieldTypes(types);
        List<GameAttrTemplate> inputTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
        List<JSONObject> defInputObjects = new ArrayList<>();
        defInputObjects.addAll(jsonObjects1);
        for (JSONObject object : jsonObjects1) {
            for (GameAttrTemplate inputTemplate : inputTemplates) {
                JSONObject att = new JSONObject();
                String aavName = inputTemplate.getAavName();
                if (Objects.equals(object.getString("aavName"), inputTemplate.getAavName())) {
                    defInputObjects.remove(object);
                    String aavNameInter = object.getString("interName");
                    String interId = object.getString("interId");
                    for (Field declaredField : declaredFields) {
                        String name = declaredField.getName();
                        if (Objects.equals(aavName, name)) {
                            declaredField.setAccessible(true);
                            Object o = null;
                            try {
                                o = declaredField.get(goodsInfoAndAttrValueDto);
                            } catch (IllegalAccessException e) {
                                throw new GlobalException("3属性参数错误，请匹配后重新配置");
                            }
                            String oneValue = String.valueOf(o);
                            if (StringUtils.isNotEmpty(oneValue)) {
                                properties.put(interId,oneValue);
                            }
                        }
                    }
                }

            }
        }
        //开始匹配默认值
        for (JSONObject object : defInputObjects) {
            String defaultValue = object.getString("defaultValue");
            if (StringUtils.isNotEmpty(defaultValue)) {
                properties.put(defaultValue,"1");
            }
        }
        params.put("properties", properties);
    }

    /**
     * 开始匹配单选属性
     *
     * @param goodsInfoAndAttrValueDto
     * @param params
     * @param radioAttr
     */
    private void builderRadioAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, Map params, String radioAttr, Map<String, String> properties) {
        //========================开始匹配单选属性=====================//
        JSONObject jsonObject = JSONObject.parseObject(radioAttr);
        String interRadioReqs = jsonObject.getString("interRadioReqs");
        log.info(JSONObject.toJSONString(interRadioReqs));
        List<JSONObject> jsonObjects = JSONObject.parseArray(interRadioReqs, JSONObject.class);
        if (org.springframework.util.CollectionUtils.isEmpty(jsonObjects)) {
            throw new GlobalException("接口单选属性暂未配置");
        }
        /**
         * 1、先查询自定义中所有单选的字段
         * 2、再查该账号单选字段中的选项
         * 3、再去接口属性匹配表过滤
         */
        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        gameAttrTemplate.setFieldType("radio");
        List<GameAttrTemplate> gameAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();


        List<JSONObject> defObjects = new ArrayList<>();
        defObjects.addAll(jsonObjects);
        for (JSONObject object : jsonObjects) {
            for (GameAttrTemplate attrTemplate : gameAttrTemplates) {
                JSONObject att = new JSONObject();
                String aavName = attrTemplate.getAavName();
                for (Field declaredField : declaredFields) {
                    String name = declaredField.getName();
                    if (aavName.equals(name)) {
                        //一级属性
                        //注意 YdConfigId 对应的是 f字段对应的 id
                        if (Objects.equals(object.getLong("YdConfigId"), attrTemplate.getId())) {
                            String InterConfigId = object.getString("InterConfigId");
                            defObjects.remove(object);//如果匹配上，则去掉
                            String interRadioReqs1 = object.getString("interRadioReqs");
                            List<JSONObject> jsonObjects2 = JSONObject.parseArray(interRadioReqs1, JSONObject.class);
                            declaredField.setAccessible(true);
                            try {
                                //获取选择的值 例如  实名中的可二次
                                Object o = declaredField.get(goodsInfoAndAttrValueDto);
                                String oneValue = String.valueOf(o);
                                List<JSONObject> twoAttr = jsonObjects2.stream().filter(j -> Objects.equals(j.getString("YdConfigId"), oneValue)).collect(Collectors.toList());
                                String interConfigName = object.getString("InterConfigName");
                                String defaultValue = object.getString("defaultValue");
                                if (org.springframework.util.CollectionUtils.isEmpty(twoAttr)) {
                                    if (StringUtils.isNotEmpty(defaultValue)) {
//                                        mateMap.put("id", InterConfigId);
//                                        mateMap.put("val", defaultValue);
                                        properties.put(InterConfigId, defaultValue);
                                    }
                                }
//                                if (org.springframework.util.CollectionUtils.isEmpty(twoAttr) && StringUtils.isEmpty(defaultValue)) {
//                                    throw new GlobalException("属性" + interConfigName + " 接口要求必传，但是暂未匹配到，请设置默认值");
//                                }
                                if (!org.springframework.util.CollectionUtils.isEmpty(twoAttr)) {
                                    JSONObject jsonObject2 = twoAttr.get(0);
                                    String interConfigId2 = jsonObject2.getString("InterConfigId");
                                    properties.put(InterConfigId, interConfigId2);
                                }
                            } catch (Exception e) {
                                throw new GlobalException("单选项" + attrTemplate.getFieldName() + "匹配错误");
                            }
                        }
                    }
                }
            }
        }
        //开始匹配默认值
        for (JSONObject object : defObjects) {
            JSONObject att = new JSONObject();
            String name = object.getString("name");
            interRadioReqs = object.getString("interRadioReqs");
            jsonObjects = JSONObject.parseArray(interRadioReqs, JSONObject.class);
            for (JSONObject objects : jsonObjects) {
                if(StringUtils.isNotEmpty(objects.getString("defaultValue"))){
                    properties.put(name, objects.getString("defaultValue"));
                }
            }
        }
        params.put("properties", properties);
        //========================单选属性匹配结束=====================//
    }
    private void builderCheckAttr(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, Map params, String checkAttr, Map<String, String> properties) {
        /**
         * 1、先查询自定义中所有多选的字段
         * 2、再查该账号单选字段中的选项
         * 3、再去接口属性匹配表过滤
         */
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();
        JSONObject checkAttrJson = JSONObject.parseObject(checkAttr);
        String interCheckBoxReqs = checkAttrJson.getString("interCheckBoxReqs");
        List<JSONObject> checkJsonList = JSONObject.parseArray(interCheckBoxReqs, JSONObject.class);
        if (org.springframework.util.CollectionUtils.isEmpty(checkAttrJson)) {
            throw new GlobalException("接口多选属性暂未配置");
        }

        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        List<String> types = new ArrayList<>();
        types.add("checkbox");
        types.add("boxradio");
        gameAttrTemplate.setFieldTypes(types);
        List<GameAttrTemplate> checkAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);

        for (JSONObject object : checkJsonList) {
            String pf_ids = "";
            for (GameAttrTemplate attrTemplate : checkAttrTemplates) {

                String aavName = attrTemplate.getAavName();
                for (Field declaredField : declaredFields) {
                    String name = declaredField.getName();
                    if (aavName.equals(name)) {
                        declaredField.setAccessible(true);

//                        List<String> list2 = new ArrayList<>();
//                        for (String s : split2) {
//                            s = s.replace(" ", "");
//                            list2.add(s);
//                        }
                        Object o = null;
                        try {
                            o = declaredField.get(goodsInfoAndAttrValueDto);
                        } catch (IllegalAccessException e) {
                            throw new GlobalException("多选项" + attrTemplate.getFieldName() + "匹配错误");
                        }
                        String oneValue = String.valueOf(o);
                        String interCheckBoxReqsStr = object.getString("interCheckBoxReqs");
                        //开始匹配二级属性
                        List<JSONObject> jsonObjects2 = JSONObject.parseArray(interCheckBoxReqsStr, JSONObject.class);
                        /**
                         * 咱们的f字段  多选项都是用空格 隔开，用,号切割数组，跟匹配的数据匹配、匹配上说明此账号勾选的有此属性
                         */
                        for (JSONObject jsonObject1 : jsonObjects2) {
                            String[] split1 = oneValue.split(",");
                            String ydConfigId1 = jsonObject1.getString("YdConfigId");
                            for (String s : split1) {
                                if (Objects.equals(s, ydConfigId1)) {
                                    String InterConfigId2 = jsonObject1.getString("InterConfigId");
                                    pf_ids+=","+InterConfigId2;
                                }
                            }
                        }

                    }
                }
            }
            if (StringUtils.isNotEmpty(pf_ids)) {
                properties.put(object.getString("name"), pf_ids.substring(1).trim());
            }
        }
    }

    /**
     * 开始匹配区服属性
     *
     * @param goodsInfoAndAttrValueDto
     * @param params
     * @param server
     */
    private void builderServer(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, Map params, String server) {
        //========================开始匹配区服属性=====================//
        JSONObject jsonObject = JSONObject.parseObject(server);
        //开始匹配一级区服
        String interServeReqs = jsonObject.getString("interServeReqs");
        String qufuid = "";
        if (Objects.equals(goodsInfoAndAttrValueDto.getGameType(), "0")) {
            qufuid = goodsInfoAndAttrValueDto.getSystemId() + "" + goodsInfoAndAttrValueDto.getCarrierId() + "";
        } else if (Objects.equals(goodsInfoAndAttrValueDto.getGameName(), "穿越火线")) {
            qufuid = goodsInfoAndAttrValueDto.getGroupId() + "";
        } else if (Objects.equals(goodsInfoAndAttrValueDto.getGameName(), "永劫无间")) {
            qufuid = goodsInfoAndAttrValueDto.getGroupId() + "";
        } else if (Objects.equals(goodsInfoAndAttrValueDto.getGameName(), "无畏契约")) {
            qufuid = goodsInfoAndAttrValueDto.getGroupId() + "";
        } else {
            qufuid = goodsInfoAndAttrValueDto.getServerId() + "";
        }
        List<JSONObject> oneServer = JSONObject.parseArray(interServeReqs, JSONObject.class);

        JSONObject oneServerJson = new JSONObject();
        for (JSONObject object : oneServer) {
            String ydServeId = object.getString("YdServeId");
            if (StringUtils.isNotEmpty(ydServeId)) {
                ydServeId = ydServeId.replace(",", "").replace(" ", "");
                if (ydServeId.contains(qufuid)) {
                    oneServerJson = object;
                    break;
                }
            }
        }
        if (Objects.isNull(oneServerJson)) {
            throw new GlobalException("运营商未匹配到");
        }
        params.put("partition_name", oneServerJson.getString("InterServeId"));
    }

    private Map<String, Object> params(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        Map<String, Object> params = new TreeMap<>();
        params.put("open_id", salePartner.getTwoOther());
        params.put("key", salePartner.getSaleKey());
        String goods_title = goodsInfoAndAttrValueDto.getDes().length() > 199 ? goodsInfoAndAttrValueDto.getDes().substring(0, 199) : goodsInfoAndAttrValueDto.getDes();
        String goods_desc = goodsInfoAndAttrValueDto.getDes().length() > 1024 ? goodsInfoAndAttrValueDto.getDes().substring(0, 1000) : goodsInfoAndAttrValueDto.getDes();
        params.put("goods_title", goods_title);
        params.put("goods_desc", goods_desc);
        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
        goodsInfoImg.setType(Constants.STR_1);
        List<GoodsInfoImg> infoImgs = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);
        String medias = infoImgs.stream().map(info -> info.getImgUrl()).collect(Collectors.toList()).stream().collect(Collectors.joining(","));
        if (StringUtils.isNotEmpty(medias)) {
        } else {
            medias = LCConstant.defaultImg;
        }
        if (StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getTitleImg())) {
            medias = goodsInfoAndAttrValueDto.getTitleImg() + "," + medias;
        }
        params.put("medias", medias);
        params.put("price", goodsInfoAndAttrValueDto.getPrice().stripTrailingZeros().toPlainString());
        params.put("open_goods_id", goodsInfoAndAttrValueDto.getId().toString());
        params.put("account", String.valueOf(System.currentTimeMillis()).substring(4));
        params.put("password", "123456");

        //自动匹配属性
        String substring = salePartner.getSaleName().substring(salePartner.getSaleName().indexOf("_")+1);
        InterAttr interAttr = interAttrMapper.selectInterAttrByConfig(substring, goodsInfoAndAttrValueDto.getGameId());
        params.put("game_id",interAttr.getInterGameId());
        String businessId = getBusinessId(salePartner, interAttr.getInterGameId());
        params.put("business_id",businessId);

        builderServer(goodsInfoAndAttrValueDto, params, interAttr.getServer());


        Map<String, String> properties = new HashMap<>();
        if(goodsInfoAndAttrValueDto.getGameName().equals("枪战王者")){
            if(StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF2())){
                GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF2()));
                properties.put("VIP等级",gameAttribute1.getFieldName().replace("V","").replace("无","1"));
            }
        }else if(goodsInfoAndAttrValueDto.getGameName().equals("永劫无间")){
            properties.put("账号类型",goodsInfoAndAttrValueDto.getServerName().replace("网易","").replace("帐","账"));
        }
        //开始匹配单选属性
        builderRadioAttr(goodsInfoAndAttrValueDto, params, interAttr.getRadioAttr(),properties);
        //开始匹配多选属性
        builderCheckAttr(goodsInfoAndAttrValueDto, params, interAttr.getCheckAttr(),properties);
        //开始匹配输入框属性
        builderInputAttr(goodsInfoAndAttrValueDto, params, interAttr.getInputAttr(),properties);

        params.put("status", "1");
        return params;
    }

    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, salePartner);
        String paramsStr = JSONObject.toJSONString(params);
        log.info("淘号号新增请求参数={}", paramsStr);
//        String response = HttpUtils.sendPost(salePartner.getOther() + "/open/publish", JSONObject.toJSONString(params), "application/json");

        HttpResponse execute = HttpRequest.post("http://api.hhaohao.com/open/publish").body(JSONObject.toJSONString(params)).execute();
        System.out.println("淘号号新增返回:" + execute);
        String response = execute.body();
        log.info("淘号号新增返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        String data = resultJson.getString("data");
        String msg = resultJson.getString("message");
        msg = MapUntils.unicodeToString(msg);
        Map<String, Object> result = new HashMap<>();
        if (!"0".equals(resultJson.getString("err_code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
            result.put("outId", JSONObject.parseObject(data).getString("goods_id"));
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", msg);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        //调新增
        Map<String, Object> action = new TreeMap<>();
        action.put("outGoodsNo", goodsInfoAndAttrValueDto.getThirdId());
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(salePartner.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        ArtRec artRec = artRecs.get(0);
        action.put("goodsKeyNo", artRec.getOutid());
        action.put("price", goodsInfoAndAttrValueDto.getPrice());
        Map<String, Object> params = new HashMap<>();
        params.put("proxyId", salePartner.getOther());
        params.put("timestamp", System.currentTimeMillis() + "");
        String proxyKey = salePartner.getSaleKey();
        String cover = MapUntils.getCover(action) + "timestamp=" + params.get("timestamp").toString() + "&key=" + proxyKey;
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        log.info(sign);
        params.put("action", action);
        params.put("sign", sign);
//        log.info(params);
        log.info("淘号号编辑请求参数={}", JSONObject.toJSONString(params));
//        String response = HttpUtils.sendPost(salePartner.getOther() + "/updateGoodsPrice", JSONObject.toJSONString(params), "application/json");

        String response = HttpRequest.post("http://api.hhaohao.com/updateGoodsPrice").body(JSONObject.toJSONString(params)).execute().body();
        if (StringUtils.isEmpty(response)){
            throw new GlobalException("淘号号直接返回null");
        }
        log.info("淘号号编辑返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        String msg = resultJson.getString("message");
        msg = MapUntils.unicodeToString(msg);
        Map<String, Object> result = new HashMap<>();
        if (!resultJson.getBoolean("success")) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", msg);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(salePartner.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        ArtRec artRec = artRecs.get(0);
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, salePartner);
        params.put("open_id", salePartner.getTwoOther());
        params.put("key", salePartner.getSaleKey());
        params.put("goods_id", artRec.getOutid());
        params.put("action", "2");
        String paramsStr = JSONObject.toJSONString(params);
        log.info("淘号号删除请求参数={}", paramsStr);
//        String response = HttpUtils.sendPost(salePartner.getOther() + "/open/shelf", JSONObject.toJSONString(params), "application/json");
        String response = HttpRequest.post("http://api.hhaohao.com/open/shelf").body(JSONObject.toJSONString(params)).execute().body();
        if (StringUtils.isEmpty(response)){
            throw new GlobalException("淘号号直接返回null");
        }

        log.info("淘号号删除返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        String msg = resultJson.getString("message");
        msg = MapUntils.unicodeToString(msg);
        Map<String, Object> result = new HashMap<>();
        if (!"0".equals(resultJson.getString("err_code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        artRecMapper.updateArtRec(artRec);
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", msg);
        return AjaxResult.success(result);
    }

    public static void main(String[] args) {
        Map<String, String> params = new TreeMap<>();
        params.put("open_id", "82860248740");
        params.put("key", "SHxmeChLbNQiQrmKbmxIxGGqgLuZczme");
        params.put("game_id", "69");
        params.put("business_id", "42");
//        log.info(params);
        String response = HttpUtil.doGet("http://api.hhaohao.com/open/game/properties", params, null);
//        String response = HttpUtil.doGet("http://api.hhaohao.com/open/all_game", params, null);
        log.info(response);
    }

    //只保留中文
    private String onlyHanZi(String str) {
        String reg = "[^\u4e00-\u9fa5]";
        str = str.replaceAll(reg, "");
        return str;
    }

}
